//
//  YMRentFreeView.swift
//  RentFreeDemo
//
//  Created by youmy on 2018/11/23.
//  Copyright © 2018 youmy. All rights reserved.
//

import UIKit

fileprivate let kCellId = "kCellId"

public protocol YMRentFreeViewDelegate:NSObjectProtocol {
    
    /// Framey发生改变
    ///
    /// - Parameters:
    ///   - view: YMRentFreeView
    ///   - isExpand: 是否展开 true 展开， false 关闭
    func frameDidChange(view:YMRentFreeView, isExpand:Bool)
}

public class YMRentFreeView: UIView {
    
    var tableView:UITableView?
    
    var isExpand:Bool = false
    
    public var delegate:YMRentFreeViewDelegate?
    
    public var rentList = [YMRentFreeModel]() {
        didSet{
            setData(isExpand)
        }
    }
    
    var headerV:YMRentFreeHeaderView?
    
    var footerV:YMRentFreeFooterView?
    
    
    /// 必须先设置setHeaderContent才会生效
    public var isArrowBtn:Bool? {
        didSet{
            
            isExpand = isArrowBtn ?? false
            
            if isArrowBtn ?? false {
                headerV?.delegate = self
            }else{
                headerV?.delegate = nil
            }
            setData(isArrowBtn ?? false)
        }
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        
        tableView = UITableView(frame: bounds)
        tableView?.isScrollEnabled = false
        tableView?.separatorStyle = .none
        tableView?.dataSource = self
        tableView?.delegate = self
        tableView?.register(YMRentFreeCell.self, forCellReuseIdentifier: kCellId)
        
        addSubview(tableView!)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func setHeaderContent(title:String){
        headerV?.removeFromSuperview()
        headerV = YMRentFreeHeaderView(frame: CGRect(x: 0, y: 0, width: ym_screen_width, height: 55))
        tableView?.tableHeaderView = headerV
        headerV?.titleLb.text = title
        if isArrowBtn ?? false {
            headerV?.delegate = self
        }else{
            headerV?.delegate = nil
        }
        setData(isArrowBtn ?? false)
    }
    
    public func setFooterContent(title:String?){
        
        footerV?.removeFromSuperview()
        guard let title = title else {
            footerV = nil
            tableView?.tableFooterView = UIView()
            setData(isExpand)
            return
        }
        
        footerV = YMRentFreeFooterView(frame: CGRect(x: 0, y: 0, width: ym_screen_width, height: title.isEmpty ? 10 : 31))
        tableView?.tableFooterView = footerV
        footerV?.titleLb.text = title
        setData(isArrowBtn ?? false)
    }
    
    func setData(_ empty:Bool = false){
        
        if empty {
            height = headerV != nil ? 45 : 0
            tableView?.frame = bounds
            tableView?.tableFooterView = UIView()
            tableView?.reloadData()
            return
        }
        
        let count = rentList.count
        
        let title = footerV?.titleLb.text ?? ""
        let footerH:CGFloat = title.isEmpty ? 10 : 31
        
        height = CGFloat(count * 34) + (headerV != nil ? 55 : 0) + (footerV != nil ? footerH : 0)
        tableView?.frame = bounds
        tableView?.tableFooterView = footerV
        
        tableView?.reloadData()
    }
}

extension YMRentFreeView: YMRentFreeHeaderViewDelegate {
    func arrowBtnDidClick(view: YMRentFreeHeaderView, btn: UIButton) {
        isExpand = !btn.isSelected
        setData(!btn.isSelected)
        delegate?.frameDidChange(view: self, isExpand: btn.isSelected)
    }
}

extension YMRentFreeView: UITableViewDataSource,UITableViewDelegate{
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rentList.count
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellId, for: indexPath) as! YMRentFreeCell
        cell.config(rentList[indexPath.row])
        return cell
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 34
    }
}
